home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / mattrib.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  4KB  |  164 lines

  1. /*
  2.  * Change MSDOS file attribute flags
  3.  *
  4.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  5.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  6.  * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  7.  *                     Environmental Management Office
  8.  *                     Fort Hood, TX 76544-5057
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include "msdos.h"
  13. #include "patchlevel.h"
  14.  
  15. int fd = -1;                /* the file descriptor for the device */
  16. int dir_start;                /* starting sector for directory */
  17. int dir_len;                /* length of directory (in sectors) */
  18. int dir_entries;            /* number of directory entries */
  19. int clus_size;                /* cluster size (in sectors) */
  20. char *mcwd;                /* the Current Working Directory */
  21. int fat_error;                /* FAT error detected? */
  22.  
  23. #define ADD    1
  24. #define REMOVE    (-1)
  25. #define LEAVE    0
  26.  
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     int entry, ismatch, oops, fargn, read_only, hidden, sys, archive;
  32.     int i, action, got_one, missed_one;
  33.     char *filename, *newfile, *unix_name(), drive, get_drive();
  34.     char *get_path(), *pathname, *get_name(), *fix_mcwd(), last_drive;
  35.     void exit(), dir_write(), dir_flush(), disk_flush();
  36.     struct directory *dir, *dir_read();
  37.  
  38.     oops = 0;
  39.     fargn = -1;
  40.     got_one = 0;
  41.     missed_one = 0;
  42.     archive = LEAVE;
  43.     hidden = LEAVE;
  44.     read_only = LEAVE;
  45.     sys = LEAVE;
  46.                     /* can't use getopt(3)... */
  47.     for (i = 1; i < argc; i++) {
  48.         switch (argv[i][0]) {
  49.             case '-':
  50.                 action = REMOVE;
  51.                 break;
  52.             case '+':
  53.                 action = ADD;
  54.                 break;
  55.             default:
  56.                 fargn = i;
  57.                 break;
  58.         }
  59.         if (fargn != -1)
  60.             break;
  61.  
  62.         switch (argv[i][1]) {
  63.             case 'a':
  64.             case 'A':
  65.                 archive = action;
  66.                 break;
  67.             case 'h':
  68.             case 'H':
  69.                 hidden = action;
  70.                 break;
  71.             case 'r':
  72.             case 'R':
  73.                 read_only = action;
  74.                 break;
  75.             case 's':
  76.             case 'S':
  77.                 sys = action;
  78.                 break;
  79.             default:
  80.                 oops++;
  81.                 break;
  82.         }
  83.         if (oops)
  84.             break;
  85.     }
  86.     if (argc < 3 || argv[fargn][0] == '\0' || oops) {
  87.         fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  88.         fprintf(stderr, "Usage: %s [-a|+a] [-h|+h] [-r|+r] [-s|+s] msdosfile [msdosfiles...]\n", argv[0]);
  89.         exit(1);
  90.     }
  91.     last_drive = 'x';
  92.     mcwd = fix_mcwd();
  93.  
  94.     for (i = fargn; i < argc; i++) {
  95.         drive = get_drive(argv[i]);
  96.         if (last_drive != drive) {
  97.             if (init(drive, 2)) {
  98.                 fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  99.                 missed_one++;
  100.                 continue;
  101.             }
  102.             last_drive = drive;
  103.         }
  104.         filename = get_name(argv[i]);
  105.         pathname = get_path(argv[i]);
  106.         if (subdir(drive, pathname)) {
  107.             missed_one++;
  108.             continue;
  109.         }
  110.  
  111.                     /* see if exists and do it */
  112.         ismatch = 0;
  113.         for (entry = 0; entry < dir_entries; entry++) {
  114.             dir = dir_read(entry);
  115.                     /* if empty */
  116.             if (dir->name[0] == 0x0)
  117.                 break;
  118.                     /* if erased */
  119.             if (dir->name[0] == 0xe5)
  120.                 continue;
  121.                     /* if dir or volume label */
  122.             if ((dir->attr & 0x10) || (dir->attr & 0x08))
  123.                 continue;
  124.  
  125.             newfile = unix_name(dir->name, dir->ext);
  126.  
  127.                     /* do it... */
  128.             if (match(newfile, filename)) {
  129.                 if (archive == ADD)
  130.                     dir->attr |= 0x20;
  131.                 if (archive == REMOVE)
  132.                     dir->attr &= ~0x20;
  133.                 if (hidden == ADD)
  134.                     dir->attr |= 0x02;
  135.                 if (hidden == REMOVE)
  136.                     dir->attr &= ~0x02;
  137.                 if (read_only == ADD)
  138.                     dir->attr |= 0x01;
  139.                 if (read_only == REMOVE)
  140.                     dir->attr &= ~0x01;
  141.                 if (sys == ADD)
  142.                     dir->attr |= 0x04;
  143.                 if (sys == REMOVE)
  144.                     dir->attr &= ~0x04;
  145.                 dir_write(entry, dir);
  146.                 ismatch++;
  147.                 got_one++;
  148.             }
  149.         }
  150.         if (!ismatch) {
  151.             fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  152.             missed_one++;
  153.         }
  154.     }
  155.     dir_flush();
  156.     disk_flush();
  157.     close(fd);
  158.     if (got_one && missed_one)
  159.         exit(2);
  160.     if (missed_one)
  161.         exit(1);
  162.     exit(0);
  163. }
  164.